package com.woniuxy.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/code")
public class CodeController {
	
	@RequestMapping("/getCode")
	@ResponseBody
	protected void getCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1 设置响应头：不允许浏览器缓存验证码图片
		response.setDateHeader("Expires", -1);//设置缓存时间
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		
		//2 告知浏览器返回数据的类型
		response.setContentType("image/jpeg");//媒体类型
		//
		//3 在内存中生成一张图片
		BufferedImage image = new BufferedImage(110, 30, BufferedImage.TYPE_INT_BGR);
		
		//4 向图片写数据，获取画笔
		Graphics graphics = image.getGraphics();
		
		//5 设置背景颜色（默认透明）
		graphics.setColor(Color.white);//设置画笔颜色
		graphics.fillRect(0, 0, 110, 30);//填充颜色，区域
		
		//6 画字母，数字
		String code = "";//随机生成字母或数字            做验证用，需要保存
		for (int i = 0; i < 5; i++) {
			//随机生成当前字母或数字的颜色
			Color color = generateColor();
			//随机生成当前字母或数据的字体
			Font font = generateFont();
			
			//设置画笔
			graphics.setColor(color);
			graphics.setFont(font);
			//随机生成文字
			String word = generateWord();
			
			//通过画笔将这些文字画到图片上
			code+=word;//字符串拼接
			//
			graphics.drawString(word, 20*i, 25);
			
		}
		
		//将验证码放到session里面,用session区别验证码，因为每个session都是唯一的
		request.getSession().setAttribute("code", code);
		
		//生成干扰点
		for (int i = 0; i < 5; i++) {
			Random random = new Random();
			int x = random.nextInt(110);
			int y = random.nextInt(30);
			//随机生成点的颜色
			Color color = generateColor();
			graphics.setColor(color);
			//画点
			graphics.fillOval(x, y, 2, 2);	
		}
		//画干扰线
		for (int i = 0; i < 2; i++) {
			Random random = new Random();
			int x1 = random.nextInt(110);
			int y1 = random.nextInt(30);
			int x2 = random.nextInt(110);
			int y2 = random.nextInt(30);
			graphics.setColor(generateColor());
			graphics.drawLine(x1, y1, x2, y2);
			
		}
		//将图片返回给浏览器
		ImageIO.write(image, "jpg", response.getOutputStream());
		
	}
	//随机生成颜色的方法
	private Color generateColor() {
		 Random random = new Random();
		 return new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
	}
	
	//随机字体
	private Font generateFont() {
		Random random = new Random();
		//预设字体类型
		String[] fontType = {"宋体","黑体"};
		//,"幼圆","方正姚体"
		//设置字体的风格
		int[] fontStyle = {Font.BOLD,Font.ITALIC,Font.CENTER_BASELINE};
		
		//随机生成字体，风格
		int type_index = random.nextInt(fontType.length);
		int style_index = random.nextInt(fontStyle.length);
		
		//随机生成字体
		return new Font(fontType[type_index], fontStyle[style_index], 28);
	}
	
	//随机生成文字
	private String generateWord() {
		Random random = new Random();
		//生成所有字母或者数字的数组
		char[] words = new char[62];
		
		//放小写字母
		int i=0;
		char w = 'a';
		for (i = 0; i < 26; i++) { //0-25
			words[i] =(char) (w+i);
			
		}
		//放大写字母
		w = 'A';
		for (int j=0; i < 52; i++,j++) { //26-51 ascii  A 65 a 97
			words[i] =(char) (w+j); 
			
		}
		//放入数字
		w = '0';
		for (int x=0; i < 62; i++,x++) {
			words[i] =(char) (w+x); 
						
		}
		//随机生成字母数字
		return ""+words[random.nextInt(62)];
				
	}


}
